#!/bin/sh

SHELL_PATH=`pwd`

#需要编译FFpmeg版本号
FF_VERSION="4.3.1"

SOURCE="ffmpeg-$FF_VERSION"

FF_PATH=$SHELL_PATH/$SOURCE
#输出路径
PREFIX=$SHELL_PATH/FFmpeg-android


#需要编译的Android API版本
ANDROID_API=14
#需要编译的NDK路径，NDK版本需大等于r15c
NDK=/Users/baimingjiang/libs/android-ndk-r17c

FF_CONFIGURE_FLAGS=""

FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --enable-hwaccels --disable-postproc"
FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-encoders"
# FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-encoders --enable-encoder=h264,aac,mpeg*"
FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-encoders --enable-encoder=mjpeg"
#FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-decoders --enable-decoder=h264,aac,mpeg*"
FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --enable-mediacodec --enable-decoder=h264_mediacodec,hevc_mediacodec,mpeg4_mediacodec,vp8_mediacodec,vp9_mediacodec"
# FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-muxers --enable-muxer=h264,aac,pcm_*,flv,mp4,avi,rtsp"
FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --enable-muxers"
FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-demuxers --enable-demuxer=mp4,h264,aac,avi,flv,rtsp,hls,*mpeg*,pcm_*"
FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-parsers --enable-parser=h264,aac,*jpeg*,mpeg*"
#FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-protocol=sctp,ftp,concat,icecast"
FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-bsfs --enable-bsf=aac_adtstoasc --enable-bsf=h264_mp4toannexb --enable-bsf=null --enable-bsf=noise"
FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-filters --enable-avfilter --enable-filter=anull"

#rm -rf "$PREFIX"
#rm -rf "$SOURCE"
if [ ! -r $SOURCE ]
then
    if [ ! -f "$SOURCE.tar.bz2" ]
    then
        echo "$SOURCE source not found, Trying to download..."
        curl http://www.ffmpeg.org/releases/$SOURCE.tar.bz2 | tar xj || exit 1
    else
        mkdir $FF_PATH
        tar zxvf $SHELL_PATH/"$SOURCE.tar.bz2" --strip-components 1 -C $FF_PATH || exit 1
    fi
fi

#若使用android-ndk-r15c及以上NDK需要打此补丁(修改FFmepg与NDK代码冲突)
sh $SHELL_PATH/build-ffmpeg-patch.sh $FF_PATH

cd $FF_PATH
export TMPDIR=$FF_PATH/tmpdir
mkdir $TMPDIR

#需要编译的平台:arm arm64 x86 x86_64，可传入平台单独编译对应的库
ARCHS=(arm arm64) # x86 x86_64)
TRIPLES=(arm-linux-androideabi aarch64-linux-android i686-linux-android x86_64-linux-android)
TRIPLES_PATH=(arm-linux-androideabi-4.9 aarch64-linux-android-4.9 x86-4.9 x86_64-4.9)

for i in "${!ARCHS[@]}";
do
    ARCH=${ARCHS[$i]}

    TOOLCHAIN=$NDK/toolchains/${TRIPLES_PATH[$i]}/prebuilt/darwin-x86_64
    SYSROOT=$NDK/platforms/android-$ANDROID_API/arch-$ARCH/
    ISYSROOT=$NDK/sysroot
    ASM=$ISYSROOT/usr/include/${TRIPLES[$i]}
    CROSS_PREFIX=$TOOLCHAIN/bin/${TRIPLES[$i]}-
    PREFIX_ARCH=$PREFIX/$ARCH


    TRMP_P=""
    if [ "$ARCH" = "arm" ]
    then
        FF_EXTRA_CONFIGURE_FLAGS="--disable-asm"
        FF_EXTRA_CFLAGS="-fpic -ffunction-sections -funwind-tables -fstack-protector -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fomit-frame-pointer -fstrict-aliasing -funswitch-loops -finline-limit=300"
        TRMP_P="eabi-v7a"
        PREFIX_ARCH="$PREFIX_ARCH$TRMP_P"
    elif [ "$ARCH" = "arm64" ]
    then
        if [ $ANDROID_API -lt 21 ]
        then
            continue
        else
        FF_EXTRA_CONFIGURE_FLAGS=""
        FF_EXTRA_CFLAGS="-fpic"
        TRMP_P="-v8a"
        PREFIX_ARCH="$PREFIX_ARCH$TRMP_P"
        fi
    elif [ "$ARCH" = "x86" -o "$ARCH" = "x86_64" ]
    then
        if [ "$ARCH" = "x86_64" -a $ANDROID_API -lt 21 ]
        then
            continue
        else
            FF_EXTRA_CONFIGURE_FLAGS="--disable-asm"
            FF_EXTRA_CFLAGS="-fpic -Dipv6mr_interface=ipv6mr_ifindex -fasm -Wno-psabi -fno-short-enums -fno-strict-aliasing -fomit-frame-pointer -march=k8"
        fi
    else
        echo "Unrecognized arch:$ARCH"
        exit 1
    fi

    FF_CFLAGS="-I$ASM -isysroot $ISYSROOT -D__ANDROID_API__=$ANDROID_API -U_FILE_OFFSET_BITS -O3 -pipe -Wall -ffast-math -fstrict-aliasing -Werror=strict-aliasing -Wno-psabi -Wa,--noexecstack -DANDROID"

    FF_LDFLAGS="$FF_LDFLAGS"

    echo FF_LDFLAGS=$FF_LDFLAGS
    echo FF_EXTRA_CFLAGS=$FF_EXTRA_CFLAGS
    echo FF_CONFIGURE_FLAGS=$FF_CONFIGURE_FLAGS
    echo FF_EXTRA_CONFIGURE_FLAGS=$FF_EXTRA_CONFIGURE_FLAGS

    ./configure \
    --prefix=$PREFIX_ARCH \
    --sysroot=$SYSROOT \
    --target-os=android \
    --arch=$ARCH \
    --cross-prefix=$CROSS_PREFIX \
    --enable-static \
    --disable-shared \
    --disable-indevs \
    --disable-outdevs \
    --enable-pic \
    --enable-nonfree \
    --enable-gpl \
    --disable-stripping \
    --enable-small \
    --enable-version3 \
    --enable-jni \
    --enable-cross-compile \
    --disable-runtime-cpudetect \
    --disable-doc \
    --disable-debug \
    --disable-ffmpeg \
    --disable-ffprobe \
    --disable-programs \
    --disable-ffplay \
    $FF_CONFIGURE_FLAGS \
    $FF_EXTRA_CONFIGURE_FLAGS \
    --extra-cflags="$FF_EXTRA_CFLAGS $FF_CFLAGS" \
    --extra-ldflags="$FF_LDFLAGS" \
    $ADDITIONAL_CONFIGURE_FLAG || exit 1

    make -j3 install || exit 1
    make distclean
    rm -rf "$PREFIX_ARCH/share"
    rm -rf "$PREFIX_ARCH/lib/pkgconfig"
done

echo "Android FFmpeg bulid success!"


